package HUMAN;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import com.sun.glass.events.WindowEvent;

import TOOLS.errCodeManagement;
import TOOLS.messageTool;
import VIEWS.v_TableInPanel;
import WORKPLACE.workPlace;

@SuppressWarnings("serial")
public class v_skillMatrixViewer extends v_TableInPanel {
	public JLabel Rcount = new JLabel("结果集=" + 0);
	boolean isSingleSelection = true;

	public JButton buttonList = new JButton("<html>未完<BR>成清单</html>");
	messageTool mt = new messageTool();

	int iWIDTH=-1;
	int iHEIGHT=-1;
	
	public JScrollPane scp02 = new JScrollPane();
	public JTable tab02 = new JTable() {
		/**
		 * 
		 */
		private static final long serialVersionUID = -5995834408990676010L;

		public boolean isCellEditable(int row, int column) {
			return isCellEditable;// 表格不允许被编辑
		}
	};

	public v_skillMatrixViewer() {
		myRepaint1();
	}
	
	public static void main(String[] args) throws Exception {
		JFrame jf=new JFrame();
		v_skillMatrixViewer p=new v_skillMatrixViewer();
		jf.setSize(p.getWidth()+20, p.getHeight()+50);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.addWindowListener(new WindowAdapter() { 
            public void windowClosing(WindowEvent we) { 
            	//TODO 关闭窗口时触发
            } 
        });
		jf.add(p);
		jf.setLayout(null);
		jf.setTitle("技能矩阵图");
		jf.setVisible(true);
	}
	
	public v_skillMatrixViewer(JPanel jf) {
		this.iWIDTH=jf.getWidth();
		this.iHEIGHT=jf.getHeight();
		myRepaint1();
	}

	public void setTable(JTable iTab) {
		tab01 = new JTable() {
			/**
			 * 
			 */
			private static final long serialVersionUID = -5995834408990676010L;

			public boolean isCellEditable(int row, int column) {
				return isCellEditable;// 表格不允许被编辑
			}
			
//			public Object getValueAt(int rowIndex,int columnIndex) {
//				Object ob=super.getValueAt(rowIndex, columnIndex);
//				if(ob==null) {
//					return null;
//				}else if(ob.equals("1")) {
//					return new ImageIcon("RR.png");
//				}else {
//					return ob;
//				}
//			}
		};
		tab02 = new JTable() {
			/**
			 * 
			 */
			private static final long serialVersionUID = -5995834408990676010L;

			public boolean isCellEditable(int row, int column) {
				return isCellEditable;// 表格不允许被编辑
			}
		};
		if (iTab == null) {
			tab01.removeAll();
			scp01.getViewport().removeAll();
			tab02.removeAll();
			scp02.getViewport().removeAll();
		} else {
			tab01.setModel(iTab.getModel());
			iTab.setAutoCreateRowSorter(true);
			tab01.setRowSorter(iTab.getRowSorter());
			tab01.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//			tab01.setAutoCreateRowSorter(true);
			tab02.setModel(iTab.getModel());
			for(int i=4;i<tab02.getColumnCount();i++) {
				hideCol(tab02,i);
			}
			hideCol(tab01,0);
			hideCol(tab01,1);
			hideCol(tab01,2);
			hideCol(tab01,3);
			tab02.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			scp02.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			scp01.getViewport().add(tab01);
			scp02.getViewport().add(tab02);
			tab02.setRowSorter(tab01.getRowSorter());
			if (isSingleSelection) {
				tab01.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				tab02.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			} else {
				tab01.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
				tab02.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
			}
			scp01.setVerticalScrollBar(scp02.getVerticalScrollBar());

			
			tab01.getTableHeader().setFont(new Font("", 1, (int) (2*rim+2)));
			tab01.setFont(new Font("", 0,  (int) (5.5*rim)));
			tab01.setRowHeight( (int) (6*rim+2));
			tab02.getTableHeader().setFont(new Font("", 1, (int) (2*rim+2)));
			tab02.setFont(new Font("", 0,  (int) (2*rim)));
			tab02.setRowHeight( (int) (6*rim+2));
			
			int t1W=scp01.getWidth()/9;
			for(int i=4;i<tab01.getColumnCount();i++) {
				String str=""+tab01.getColumnModel().getColumn(i).getHeaderValue();
				int colLen=5;
				if(str.length()>colLen) {
					String str1="<html><center>"+str.substring(0, colLen)+"</center><center>"
							+str.substring(colLen, str.length())+"</center></html>";
					tab01.getColumnModel().getColumn(i).setHeaderValue(str1);
					tab01.getTableHeader().setPreferredSize(new Dimension(10000, 
							tab01.getTableHeader().getFont().getSize()*4));
					tab02.getTableHeader().setPreferredSize(tab01.getTableHeader().getPreferredSize());
				}
				tab01.getColumnModel().getColumn(i).setPreferredWidth(t1W);
			}
			tab01.updateUI();
			tab02.getColumnModel().getColumn(0).setPreferredWidth(scp02.getWidth()/5);
			tab02.getColumnModel().getColumn(1).setPreferredWidth((scp02.getWidth()/5*4) / 3);
			tab02.getColumnModel().getColumn(2).setPreferredWidth((scp02.getWidth()/5*4) / 3);
			tab02.getColumnModel().getColumn(3).setPreferredWidth((scp02.getWidth()/5*4) / 3);
			
			setTab_skillcircle();
			
			
			tab02.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					super.mouseClicked(event);
					try {
						int iRow = tab02.getSelectedRow();
						if (iRow >= 0) {
							tab01.setRowSelectionInterval(iRow, iRow);
						}
					} catch (Exception e) {
						String str = errCodeManagement.getErrText(e.getMessage());
						mt.show(str);
					}
				}

				public void mouseReleased(MouseEvent event) {
					super.mouseReleased(event);
					try {
						int iRow = tab02.getSelectedRow();
						if (iRow >= 0) {
							tab01.setRowSelectionInterval(iRow, iRow);
						}
					} catch (Exception e) {
						String str = errCodeManagement.getErrText(e.getMessage());
						mt.show(str);
					}
				}
			});
			tab01.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					super.mouseClicked(event);
					try {
						int iRow = tab01.getSelectedRow();
						if (iRow >= 0) {
							tab02.setRowSelectionInterval(iRow, iRow);
						}
					} catch (Exception e) {
						String str = errCodeManagement.getErrText(e.getMessage());
						mt.show(str);
					}
				}

				public void mouseReleased(MouseEvent event) {
					super.mouseReleased(event);
					try {
						int iRow = tab01.getSelectedRow();
						if (iRow >= 0) {
							tab02.setRowSelectionInterval(iRow, iRow);
						}
					} catch (Exception e) {
						String str = errCodeManagement.getErrText(e.getMessage());
						mt.show(str);
					}
				}
			});
			
		}
	}
	
	public void setTab_skillcircle() {
		try {
			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
 
				/**
				 * 
				 */
				private static final long serialVersionUID = 1522678096361273273L;

				public Component getTableCellRendererComponent(JTable table,
						Object value, boolean isSelected, boolean hasFocus,
						int row, int column) {
					if(column>3) {//	◔	◑	◕	●	
						if(value==null) {
							value=null;
							setIcon(null);
						}else if(value.equals("1")) {
							if(workPlace.img_skill_lv1==null) {
								value="◔";
							}else {
								value=null;
								setIcon(workPlace.img_skill_lv1);
							}
						}else if(value.equals("2")) {
							if(workPlace.img_skill_lv2==null) {
								value="◑";
							}else {
								value=null;
								setIcon(workPlace.img_skill_lv2);
							}
						}else if(value.equals("3")) {
							if(workPlace.img_skill_lv3==null) {
								value="◕";
							}else {
								value=null;
								setIcon(workPlace.img_skill_lv3);
							}
						}else if(value.equals("4")) {
							if(workPlace.img_skill_lv4==null) {
								value="●";
							}else {
								value=null;
								setIcon(workPlace.img_skill_lv4);
							}
						}else {
							setIcon(null);
						}
					}
					return super.getTableCellRendererComponent(table, value,
							isSelected, hasFocus, row, column);
				}
				
				
				
			};
			tcr.setHorizontalAlignment(JLabel.CENTER);// 设置table内容居中
			int columnCount = tab01.getColumnCount();
			for (int i = 0; i < columnCount; i++) {
				tab01.getColumnModel().getColumn(i).setCellRenderer(tcr);
				
			}
			tab01.updateUI();
			tcr=null;
		} catch (Exception ex) {
//			ex.printStackTrace();
		}
	}
	
	public void hideCol(JTable inTab,int hideIndex) {
		TableColumn idColumn= inTab.getColumnModel().getColumn(hideIndex);
		idColumn.setWidth(0);
		idColumn.setMaxWidth(0);
		idColumn.setMinWidth(0);
		inTab.getTableHeader().getColumnModel().getColumn(hideIndex).setMaxWidth(0); //设置表的标题的宽度也为0,这个很重要
		inTab.getTableHeader().getColumnModel().getColumn(hideIndex).setMinWidth(0);
	}

	public void myRepaint1() {
		int sw = Toolkit.getDefaultToolkit().getScreenSize().width;
		int sh = Toolkit.getDefaultToolkit().getScreenSize().height;
		WIDTH = (int) (sw * 0.8);
		this.HEIGHT = (int) (sh * 0.8);
		if(iWIDTH>0) {
			WIDTH=iWIDTH;
		}
		if(iHEIGHT>0) {
			HEIGHT=iHEIGHT;
		}
		buttonWid = 5 * fieldsHei;
		this.columns = 3;
		super.myRepaint();
		this.labels[0].setText("工号: ");
		this.labels[1].setText("名称: ");
		this.labels[2].setText("班别: ");

		this.buttonAdd.setText("新增");
		this.buttonAdd.setEnabled(false);
		this.buttonAdd.setVisible(false);
		this.buttonSave.setText("保存");
		this.buttonSave.setEnabled(false);
		this.buttonSave.setVisible(false);
		this.buttonDelete.setEnabled(false);
		this.buttonDelete.setVisible(false);
		this.buttonEdit.setText("<html>培训报名</html>");
		this.buttonEdit.setEnabled(false);
		this.buttonEdit.setVisible(false);
		this.buttonView.setText("<html>培训开始</html>");
		this.buttonView.setEnabled(false);
		this.buttonView.setVisible(false);
		this.buttonList.setText("<html>未完成<BR>清单</html>");
		this.buttonList.setBounds(buttonView.getX(), buttonView.getY() + buttonView.getHeight() + rim,
				buttonView.getWidth(), buttonView.getHeight());
		this.add(buttonList);
		this.buttonList.setVisible(false);

		this.buttonCancel.setEnabled(false);
		this.buttonCancel.setVisible(false);
		this.buttonCancel.setText("已离职");
		this.isCellEditable = true;

		scp01.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		double colRate=0.25;
		scp01.setBounds((int) (WIDTH * colRate + rim), scp01.getY(),
				(int) (this.buttonSearch.getX() - 2 * rim - WIDTH * colRate), scp01.getHeight());
		scp02.setBounds(rim, scp01.getY(), (int) (WIDTH * colRate), scp01.getHeight());
		this.add(scp02);
		scp02.getViewport().add(tab02);
	}
}
